import { ref } from 'vue'
import { defineStore } from 'pinia'
import { createTestLoaderCloudStorage } from '@/lib/test'
import type { ITestPaperBaseData } from '@/lib/test'


export const usePaperStore = defineStore('paper', () => {

    // 试卷加载器
    const testLoader = createTestLoaderCloudStorage('http://test.code9.top/question-bank')

    // 载入状态
    const isLoading = ref(false)

    // 同步状态
    const isSyncing = ref(false)

    // 试卷数组
    const papers = ref<ITestPaperBaseData[]>([])

    // 同步题库
    const testSync = async () => {
        isSyncing.value = true
        await testLoader.testSync()
        isSyncing.value = false
    }

    // 载入试卷基础信息列表
    const loadPaperBaseDataList = async () => {
        isLoading.value = true
        const paperBaseDataList = await testLoader.loadPaperBaseDataList()
        papers.value = paperBaseDataList
        isLoading.value = false
    }

    // 载入试卷
    const loadPaper = async (paperName: string) => {
        return await testLoader.loadPaper(paperName)
    }

    // 删除试卷
    const removePaper = async (paperName: string) => {
        await testLoader.removePaper(paperName)
    }


    return { papers, isLoading, isSyncing, testSync, loadPaper, loadPaperBaseDataList, removePaper }

})
